Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java Panama FFI prototype experiments (DO NOT MERGE) #11095

Draft
wants to merge 55 commits into
base: main
Choose a base branch
from

Conversation

alanpaxton
Copy link
Contributor

@alanpaxton alanpaxton commented Jan 17, 2023

Experimental implementation of Java 19 PREVIEW API JEP 424, aka Project Panama based RocksDB Java API.

See Java Panama FFI prototype Detailed Description

  • Re-imagining of the RocksDB Java API based on new Memory and Foreign Function Interface support
  • Replaces our use of JNI
  • Requires Java 19 (the API is planned to become stable in Java 21)
  • This is based on a preview API and is therefore not presently usable in production

Goals

  • Demonstrate a proof-of-concept of a RocksDB Java API based on Panama FFI, for basic API features (get())
  • Use/implement up-to-date features of RocksDB core API (e.g. expose PinnableSlice at API level)
  • Measure the performance of this API

Non-Goals

  • full API implementation
  • merge of this work into RocksDB main

Implementing a get() using FFI
We are building and accessing function(s)
(1) Still working out why the data is not coming back, as it has been read into the PinnableSlice and copied to the rocksdb_output_slice
(2) Make classes with VarHandles as members for repeated, efficient access to slices..
- why do we need to pass memory addresses ?
- lots of refactor/cleanup yet to do..
Pull out MethodHandles, MemoryLayouts and VarHandles used by the RocksDB FFI so that the core of the methods in FFIDB (get(), etc) are compact and readable.

Make the first test check the returned value of a key originally written by the old API.
Change FFI API into a byte[]-based one from String, consistent with the current API (String was just for initial testing)
Make reset() of pinnable slice public (it shoud be, and jmh benchmarks need it to be)
Build configurations need updated to build RocksDB JAR in java 19 and enable preview features. Same for jmh test jar, and invocations
This should mean that allocation is done with a default Cleaner linked to the session which should release the allocated segments via GC. We seem to be getting away with it in jmh GetBenchmark.java
JMH benchmarks are sufficiently good that we have implemented a paranoid test confirming that multiple get()s are all really working and fetching back from a database.

Start commenting the important methods and classes.
Investigate whether mutiple calls over FFI are a problem; we have to do 2 calls per op when using pinnable slices, 2nd to release the slice.
So we implement a copying get (getOutputSlice) that obviously doesn’t need to hold a pinnable slice back in Java; It may be more efficient for small value sizes.
Describes what we have done and contains some results.
These are still part of exploring why FFI implementations are slightly slower than JNI.
Summary at current state, where we are still at a loss as to wny performance is not as good as with JNI. We have tested some hypotheses, but none is able to account for the difference.
Allocate a single segment that contains both input and output slice structs
Avoid copying and recycling for a potentially fairer benchmarking.
Since we are not using it as heavily;
and because our recycling allocation is not happy when we hold something long-lived.
Allocate one separately at the start
reset() it when needed
dispose at the end
Try performance again
Remove the custom allocator that we don’t use any more; we have reduced allocation use to a mimmum instead.
Type things a little better where we pass segment addresses to method invocation.
Polish error/exception messages.
Name parameters consistently.
Make GetParams auto-closeable.
Type checking is tighter.
Does it help performance ? It can’t hurt.
On M1 Max, maybe 1% performance gain with 64k value size. There’s something there, if not much.
We can just fill the key segment with the bytes we need.
Make all VarHandle(s) conform to exact invocation.
Reflect the fact performance is now very nearly equal in the writeup, acknowledge help from Maurizio at Oracle. (panama-dev).
Analyse performance cost of the extra FFI call to release a pinnable slice versus the extra copy into a foreign memory segment allocated on the Java side. 1k-4k break even.
First part of replacement is to not do anything with the replaced SliceBuffer, just check the CI run..
facebook-github-bot pushed a commit that referenced this pull request Feb 20, 2024
Summary:
We did some experimental work with FFI and native memory as a potential improvement to the Java API.
The work lives (unmerged) in #11095

This is the report text from that branch, extract as a blog post.
Along with some supporting files (png, pdf of graphs).

Pull Request resolved: #11760

Reviewed By: hx235

Differential Revision: D53943442

Pulled By: ajkr

fbshipit-source-id: 7c9f800e25be22c10e736cdd3b0d65422ecfc826
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants